import 'package:flutter/material.dart';

class _AppStateObserver extends WidgetsBindingObserver {
  final VoidCallback onAppResumed;
  final VoidCallback onAppPaused;

  _AppStateObserver(this.onAppResumed, this.onAppPaused);

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print("AppLifecycleState = $state");
    if (state == AppLifecycleState.resumed) {
      onAppResumed();
    } else if (state == AppLifecycleState.paused) {
      onAppPaused();
    }
  }
}

mixin AppLifecycleObserverMixin<T extends StatefulWidget> on State<T> {
  late _AppStateObserver _observer;

  @override
  void initState() {
    super.initState();
    _observer = _AppStateObserver(onDidResumed, onDidPaused);
    WidgetsBinding.instance!.addObserver(_observer);
    onDidResumed();
  }

  @override
  void dispose() {
    WidgetsBinding.instance!.removeObserver(_observer);
    super.dispose();
  }

  void onDidResumed() {}

  void onDidPaused() {}
}
